package com.example.controller;

import com.example.entity.Department;
import com.example.entity.example.DepartmentExample;
import com.example.service.DepartmentService;
import com.example.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/department")
@Controller
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/get.json")
    public ResultEntity<List<Department>> get(@RequestParam("page") Integer page,
                                              @RequestParam("limit") Integer limit,
                                              @RequestParam(value = "id", required = false) Integer id,
                                              @RequestParam(value = "name", required = false) String name
    ) {
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !name.isEmpty()) {
            name = '%' + name + '%';
            criteria.andNameLike(name);
        }
        List<Department> departments = departmentService.getByExampleWithLimit(example, page, limit);
        long count = departmentService.getCountByExample(example);
        return ResultEntity.createResultEntity(0, null, count, departments);
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @RequestMapping("/add/page.html")
    public String addPage() {
        return "department/department-add";
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/add")
    public ResultEntity<Object> add(@RequestBody Department department) {
        departmentService.add(department);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @RequestMapping("/{id}/modify/page.html")
    public String modifyPage(ModelMap modelMap, @PathVariable("id") Integer id) {
        Department department = departmentService.getById(id);
        modelMap.addAttribute("department", department);
        return "department/department-modify";
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity<Object> update(@RequestBody Department department) {
        departmentService.updateById(department);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/remove")
    public ResultEntity<Object> remove(@RequestParam("id") Integer id) {
        departmentService.removeById(id);
        return ResultEntity.successWithoutData();
    }
}
